package uc.org.jkas.model;
import org.jkas.core.Model;
import java.util.HashMap;
import java.util.ArrayList;
import uc.org.jkas.model.PrivilegesM;
public class RolesM extends Model
{ 
	public RolesM()
	{
	 primaryKey("role_id");
	}

	public HashMap<String,String> getByName(String caption)
	{   HashMap<Integer,Object> params=new HashMap<Integer,Object>();
	    params.put(1,caption);
		return this.where("caption=?").prepareRows(params);
	}

	public boolean exists(String caption)
	{return exists(caption,0);
	}

	public boolean exists(String caption,int role_id)
	{boolean res=false;
	   HashMap<String,String> info=null;
			      this.where(strc("caption='",caption,"'"));
					info=this.getRows();
					if(info!=null && !info.isEmpty())
		             { if(role_id>0 && role_id==toInt(info.get("role_id")))
						 {res=false;}
					     else
						 {
						   res=true;
						 }
		             }
	 return res;
	}

	public String getPrivis(String role_id)
	{HashMap<String,String> info=this.getInfo(role_id);
	  PrivilegesM psm=new PrivilegesM();
	  ArrayList<HashMap> privis=psm.where(strc("privi_id in(",info.get("privileges"),")")).select();
	  StringBuilder sp=new StringBuilder();
	    for(HashMap priv:privis)
		{sp.append(priv.get("control")).append("_").append(priv.get("action")).append("|");
		}
	  return sp.toString();
	}
}